package com.lancer.springbootdemo.tacos.web;

import com.lancer.springbootdemo.tacos.beans.Ingredient;
import com.lancer.springbootdemo.tacos.beans.Taco;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import com.lancer.springbootdemo.tacos.beans.Ingredient.Type;


import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j // 自动注入日志方法，来自Lmobok
@Controller // 控制器
@RequestMapping("/design") // 构造器中所有请求的前缀路径
public class DesignTacoController {

    @GetMapping
    public String showDesignForm(Model model){
        List<Ingredient> ingredients = Arrays.asList(
                new Ingredient("FLTO", "玉米粉薄烙饼", Type.WRAP),
                new Ingredient("COTO", "玉米薄饼", Type.WRAP),
                new Ingredient("GRBF", "绞碎的牛肉", Type.PROTEIN),
                new Ingredient("CARN", "Carnitas", Type.PROTEIN),
                new Ingredient("TMTO", "蕃茄丁", Type.VEGGIES),
                new Ingredient("LETC", "生菜", Type.VEGGIES),
                new Ingredient("CHED", "切达干酪", Type.CHEESE),
                new Ingredient("JACK", "Monterrey Jack", Type.CHEESE),
                new Ingredient("SLSA", "萨尔萨舞", Type.SAUCE),
                new Ingredient("SRCR", "酸奶油", Type.SAUCE)
        );
        Type[] types = Ingredient.Type.values();
        for(Type type : types){
            model.addAttribute(type.toString().toLowerCase(),filterByType(ingredients,type));
        }
        model.addAttribute("design",new Taco());
        return "design";// 返回design视图
    }


    /**
     * 过滤食材对应的食物
     * @param ingredients
     * @param type
     * @return
     */
    private List<Ingredient> filterByType(List<Ingredient> ingredients, Type type){
        // lamda表达式筛选list
        return ingredients.stream()
                .filter(x -> x.getType().equals(type))
                .collect(Collectors.toList());
    }



    @PostMapping
    public String processDesign(Taco taco){
        log.info("Process Design: " + taco);
        return "redirect:/orders/current";
    }

}
